//
//  HMPullDownRefreshControl.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/17.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "HMPullDownRefreshControl.h"
#define HMPullDownRefreshControlHeight 60

typedef  NS_ENUM(NSInteger,HMPullDownRefreshControlStatue)
{
    HMPullDownRefreshControlNormal, //没达到刷新的程度
    HMPullDownRefreshControlPullToRefresh, //下拉来刷新
    HMPullDownRefreshControlRefreshing, //刷新中
};

@interface HMPullDownRefreshControl ()

@property(nonatomic,assign)HMPullDownRefreshControlStatue statue;
@property(nonatomic,strong)UIScrollView *superScrollView;
@property(nonatomic,strong)UIImageView *circleView;
@property(nonatomic,strong)UIImageView *arrowView;
@property(nonatomic,strong)UILabel *messageLabel;


@end

@implementation HMPullDownRefreshControl


-(instancetype)initWithFrame:(CGRect)frame
{
    CGRect newFrame = CGRectMake(0, -HMPullDownRefreshControlHeight, [UIScreen mainScreen].bounds.size.width, HMPullDownRefreshControlHeight);
    
    if(self = [super initWithFrame:newFrame])
    {
        [self setupUI];
    }
    return self;
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if ([newSuperview isKindOfClass:[UIScrollView class]])
    {
        self.superScrollView = (UIScrollView *)newSuperview;
        
        [newSuperview addObserver:self forKeyPath:@"contentOffset" options:0 context:nil];
    }
}

-(void)setupUI
{
    //    1.添加子控件
    [self addSubview:self.circleView];
    [self addSubview:self.arrowView];
    [self addSubview:self.messageLabel];
    
    self.circleView.hidden = YES;
    
//    2.添加约束
    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).offset(-30);
    }];
    
    [self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.circleView);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.circleView.mas_right).offset(10);
    }];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if (self.superScrollView.dragging)
    {
        CGFloat standarHeight = -(self.superScrollView.contentInset.top + HMPullDownRefreshControlHeight);
        if (self.statue == HMPullDownRefreshControlNormal && self.superScrollView.contentOffset.y < standarHeight)
        {
            self.statue = HMPullDownRefreshControlPullToRefresh;
        }else if (self.statue == HMPullDownRefreshControlPullToRefresh && self.superScrollView.contentOffset.y > standarHeight)
        {
            self.statue = HMPullDownRefreshControlNormal;
        }
    }else if(self.statue == HMPullDownRefreshControlPullToRefresh)
    {
        self.statue = HMPullDownRefreshControlRefreshing;
    }
}


-(void)setStatue:(enum HMPullDownRefreshControlStatue)statue
{
    _statue = statue;
    
    if (statue == HMPullDownRefreshControlNormal) {
        
        self.circleView.hidden = YES;
        self.arrowView.hidden = NO;
        
        self.messageLabel.text = @"下拉刷新";
        [UIView animateWithDuration:HMAnimationInterver animations:^{
             self.arrowView.transform = CGAffineTransformIdentity;
        }];
    }
    else if (statue == HMPullDownRefreshControlPullToRefresh)
    {

        self.messageLabel.text = @"释放刷新";
        
        [UIView animateWithDuration:HMAnimationInterver animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
    }
    else
    {
        self.messageLabel.text = @"正在刷新";
       
         self.arrowView.transform = CGAffineTransformIdentity;
        self.arrowView.hidden = YES;
        self.circleView.hidden = NO;
        
        CABasicAnimation *aniamtion = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        aniamtion.toValue =  @(M_PI * 2);
        aniamtion.duration = 0.5;
        aniamtion.repeatCount = MAXFLOAT;
        
        aniamtion.removedOnCompletion = NO;
        [self.circleView.layer addAnimation:aniamtion forKey:@"animation"];
        
//        让tableView往下走导航条的高度,这样就不会被导航条挡住
        [UIView animateWithDuration:HMAnimationInterver animations:^{
            
            UIEdgeInsets contentInset = self.superScrollView.contentInset;
            contentInset.top += HMPullDownRefreshControlHeight;
            self.superScrollView.contentInset = contentInset;
            
        }];
        
//        提示控制器加载数据
        self.actionHandleBlock();
    }
}

-(void)startRefreshing
{
//    将状态切换到Refresh
    self.statue = HMPullDownRefreshControlRefreshing;
}

-(void)stopRefreshing
{
    self.statue = HMPullDownRefreshControlNormal;
    
    [UIView animateWithDuration:HMAnimationInterver animations:^{
        
        UIEdgeInsets contentInset = self.superScrollView.contentInset;
        contentInset.top -= HMPullDownRefreshControlHeight;
        self.superScrollView.contentInset = contentInset;
        
    }];
}

#pragma mark - 懒加载控件
-(UIImageView *)circleView
{
    if (_circleView == nil)
    {
        self.circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableview_loading"]];
    }
    return _circleView;
}

-(UIImageView *)arrowView
{
    if(_arrowView == nil)
    {
        self.arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableview_pull_refresh"]];
    }
    return _arrowView;
}

-(UILabel *)messageLabel
{
    if (_messageLabel == nil)
    {
        self.messageLabel = [[UILabel alloc] init];
        self.messageLabel.textAlignment = NSTextAlignmentCenter;
        self.messageLabel.font = [UIFont systemFontOfSize:14];
        self.messageLabel.text = @"下拉刷新";
        
        self.messageLabel.textColor = [UIColor darkGrayColor];
    }
    return _messageLabel;
}

-(void)dealloc
{
    [self.superScrollView removeObserver:self forKeyPath:@"contentOffset"];
   
}

@end












